/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HoaDon.java
 *
 * Created on Nov 11, 2012, 12:53:18 PM
 */
package doan;

import Dao.CTHoaDonDao;
import Dao.SanPhamDao;
import Pojo.CTHoaDonPojo;
import Pojo.SanPhamPojo;
import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.Default;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author LETHOM
 */
public class ChiTietHoaDon extends javax.swing.JFrame {

    public int _mahoadon = 1;
    int _masanpham = -1;//-1 la gia tri mac dinh co nghia la khong co san pham
    int _machitiethoadon = -1;
    int _soluong = 0;
    double _giaban = 0;
    DefaultTableModel model;
    ArrayList<SanPhamPojo> dssp;
    ArrayList<CTHoaDonPojo> ds;

    /** Creates new form HoaDon */
    public ChiTietHoaDon() {
        initComponents();
        jTextField1.setText("");
        jTextField3.setText("");
        dssp = SanPhamDao.DanhSachSanPham();
        if (dssp.isEmpty() == false) {
            jComboBox3.removeAllItems();
            for (int i = 0; i < dssp.size(); i++) {
                jComboBox3.addItem(dssp.get(i).getMaSP());
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblDSSP = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jComboBox3 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                FLoad(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24));
        jLabel1.setText("Chi tiết Hóa Đơn");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Danh Sách Sản Phẩm");

        jtblDSSP.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "STT", "Mã sản phẩm", "Số lượng", "Giá"
            }
        ));
        jtblDSSP.setUpdateSelectionOnSort(false);
        jtblDSSP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblDSSPMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtblDSSP);

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));

        jLabel3.setText("Mã sản phẩm");

        jLabel4.setText("Giá");

        jLabel5.setText("Số lượng");

        jTextField1.setText("jTextField1");

        jTextField3.setText("jTextField3");

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Thêm");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Sửa");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Xóa");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(32, 32, 32)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addGap(15, 15, 15)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(jTextField3)
                            .addComponent(jComboBox3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(34, 34, 34)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addGap(57, 57, 57))
        );

        jButton5.setText("Thoat");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton5))))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jButton5)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void FLoad(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_FLoad
        // TODO add your handling code here:
        FLoadDS();
    }//GEN-LAST:event_FLoad

    /**
     * Load danh sach chi tiet hoa don vao bang chi tiet
     */
    void FLoadDS() {
        ds = CTHoaDonDao.FChiTietHoaDonTheoMaHoaDon(_mahoadon);
        if (ds.isEmpty() == false) {
            model = (DefaultTableModel) jtblDSSP.getModel();
            model.getDataVector().clear();
            for (int i = 0; i < ds.size(); i++) {
                Vector v = new Vector();
                v.addElement(ds.get(i).getMaCTHD());
                v.addElement(ds.get(i).getMaSP());
                v.addElement(ds.get(i).getSoLuong());
                v.addElement(ds.get(i).getGiaBan());
                model.addRow(v);
            }
            jtblDSSP.setModel(model);
        }
    }
    private void jtblDSSPMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblDSSPMouseClicked
        // TODO add your handling code here:
        int row = jtblDSSP.getSelectedRow();
        CTHoaDonPojo cthdp = ds.get(row);
        jComboBox3.setSelectedItem(cthdp.getMaSP());
        _masanpham = cthdp.getMaSP();
        jTextField1.setText(cthdp.getSoLuong() + "");
        jTextField3.setText(cthdp.getGiaBan() + "");
    }//GEN-LAST:event_jtblDSSPMouseClicked
    /**
     * Kiem tra du lieu nhap vao co dung khong
     * @return 
     */
    public boolean FKiemTraNhapLieu() {
        String strSoLuong = "", strGiaBan = "";
        strSoLuong = jTextField1.getText();
        strGiaBan = jTextField3.getText();
        try {
            _soluong = Integer.parseInt(strSoLuong);
            _giaban = Double.parseDouble(strGiaBan);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(rootPane, "Số lượng phải là số nguyên.\nGiá bán phải là giá tiền cụ thể.", "Thông báo", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    public boolean FKiemTraSPDaCoChua(int masanpham) {
        for (int i = 0; i < ds.size(); i++) {
            if (ds.get(i).getMaSP() == masanpham) {
                _machitiethoadon = ds.get(i).getMaCTHD();
                return true;
            }
        }
        return false;
    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (FKiemTraNhapLieu()) {
            _masanpham = (Integer) jComboBox3.getSelectedItem();
            if (!FKiemTraSPDaCoChua(_masanpham)) {
                CTHoaDonPojo cthdp = new CTHoaDonPojo(_mahoadon, _masanpham, _soluong, _giaban);
                int kq = CTHoaDonDao.FThemCTHD(cthdp);
                JOptionPane.showMessageDialog(rootPane, "Them chi tiet thanh cong.");
                FLoadDS();
            } else {
                JOptionPane.showMessageDialog(rootPane, "San pham da co trong hoa don.");
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if (FKiemTraNhapLieu()) {
            _masanpham = (Integer) jComboBox3.getSelectedItem();
            if (FKiemTraSPDaCoChua(_masanpham)) {
                CTHoaDonPojo cthdp = new CTHoaDonPojo(_machitiethoadon, _mahoadon, _masanpham, _soluong, _giaban);
                CTHoaDonDao.CapNhatCTHD(cthdp);
                FLoadDS();
            } else {
                JOptionPane.showMessageDialog(rootPane, "San pham chua co trong hoa don.");
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        _masanpham = (Integer) jComboBox3.getSelectedItem();
         if (FKiemTraSPDaCoChua(_masanpham)) {
                int n=JOptionPane.showConfirmDialog(rootPane, "Ban co thuc su muon xoa san pham "+_masanpham, "Thong bao", JOptionPane.YES_NO_OPTION);
                if(n==JOptionPane.YES_OPTION){
                    CTHoaDonDao.XoaCTHD(_machitiethoadon);
                    FLoadDS();
                }
            } else {
                JOptionPane.showMessageDialog(rootPane, "San pham chua co trong hoa don.");
            }
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ChiTietHoaDon().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTable jtblDSSP;
    // End of variables declaration//GEN-END:variables
}
